package com.goldze.mvvmhabit.data.source.http;

import com.goldze.mvvmhabit.data.source.HttpDataSource;
import com.goldze.mvvmhabit.data.source.http.service.DemoApiService;
import com.goldze.mvvmhabit.entity.DemoEntity;
import com.goldze.mvvmhabit.entity.request.AddLeaveFormRequestEntity;
import com.goldze.mvvmhabit.entity.request.ChangePswRequestEntity;
import com.goldze.mvvmhabit.entity.request.DelectFriendEntity;
import com.goldze.mvvmhabit.entity.request.ForgetPswRequestEntity;
import com.goldze.mvvmhabit.entity.request.GetHistoryMsgRequestEntity;
import com.goldze.mvvmhabit.entity.request.GetLeaveListRequestEntity;
import com.goldze.mvvmhabit.entity.request.LoginRequestEntity;
import com.goldze.mvvmhabit.entity.request.ModifyDepartRequestEntity;
import com.goldze.mvvmhabit.entity.request.RegisterRequestEntity;
import com.goldze.mvvmhabit.entity.request.SendMsgRequestEntity;
import com.goldze.mvvmhabit.entity.request.SendSmsCodeRequestEntity;
import com.goldze.mvvmhabit.entity.request.SetHasReadRequestEntity;
import com.goldze.mvvmhabit.entity.request.SexRequestEntity;
import com.goldze.mvvmhabit.entity.request.UploadHeadRequestEntity;
import com.goldze.mvvmhabit.entity.response.CompanyResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetBannerListResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetChatUsersResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetHistoryMsgResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetLeaveListResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetNewMsgResponseEntity;
import com.goldze.mvvmhabit.entity.response.GetTypeListResponseEntity;
import com.goldze.mvvmhabit.entity.response.LoginResponseEntity;
import com.goldze.mvvmhabit.entity.response.RegisterResponseEntity;
import com.goldze.mvvmhabit.entity.response.SelectDepartmentResponseEntity;
import com.goldze.mvvmhabit.entity.response.UserInfoResponseEntity;
import com.goldze.mvvmhabit.entity.response.VersionResponseEntity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import me.goldze.mvvmhabit.http.BaseResponse;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by goldze on 2019/3/26.
 */
public class HttpDataSourceImpl implements HttpDataSource {
    private DemoApiService apiService;
    private volatile static HttpDataSourceImpl INSTANCE = null;

    public static HttpDataSourceImpl getInstance(DemoApiService apiService) {
        if (INSTANCE == null) {
            synchronized (HttpDataSourceImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new HttpDataSourceImpl(apiService);
                }
            }
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }

    private HttpDataSourceImpl(DemoApiService apiService) {
        this.apiService = apiService;
    }

    @Override
    public Observable<Object> login() {
        return Observable.just(new Object()).delay(3, TimeUnit.SECONDS); //延迟3秒
    }

    @Override
    public Observable<DemoEntity> loadMore() {
        return Observable.create(new ObservableOnSubscribe<DemoEntity>() {
            @Override
            public void subscribe(ObservableEmitter<DemoEntity> observableEmitter) throws Exception {
                DemoEntity entity = new DemoEntity();
                List<DemoEntity.ItemsEntity> itemsEntities = new ArrayList<>();
                //模拟一部分假数据
                for (int i = 0; i < 10; i++) {
                    DemoEntity.ItemsEntity item = new DemoEntity.ItemsEntity();
                    item.setId(-1);
                    item.setName("模拟条目");
                    itemsEntities.add(item);
                }
                entity.setItems(itemsEntities);
                observableEmitter.onNext(entity);
            }
        }).delay(3, TimeUnit.SECONDS); //延迟3秒
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoGet() {
        return apiService.demoGet();
    }

    @Override
    public Observable<BaseResponse<DemoEntity>> demoPost(String catalog) {
        return apiService.demoPost(catalog);
    }

    @Override
    public Observable<BaseResponse<LoginResponseEntity>> loginbypassword(String phone, String passWord) {
        LoginRequestEntity login = new LoginRequestEntity();
        login.setPhone(phone);
        login.setPassWord(passWord);
        return apiService.loginbypassword(login);
    }

    @Override
    public Observable<BaseResponse<UserInfoResponseEntity>> getUserInfo() {
        return apiService.getUserInfo();
    }

    @Override
    public Observable<BaseResponse<RegisterResponseEntity>> register(String phone, String code, String name,
                                                                     String settingPassword) {
        RegisterRequestEntity register = new RegisterRequestEntity();
        register.setPhone(phone);
        register.setPassWord(settingPassword);
        register.setNickName(name);
        register.setCode(code);
        return apiService.register(register);
    }

    @Override
    public Observable<BaseResponse<Boolean>> requestChangePassword(String oldPassWord, String newPassWord) {
        ChangePswRequestEntity password = new ChangePswRequestEntity();
        password.setOldPassWord(oldPassWord);
        password.setNewPassWord(newPassWord);
        return apiService.modifyPwd(password);
    }

    @Override
    public Observable<BaseResponse<String>> forgetPwd(String phone, String code, String passWord) {
        ForgetPswRequestEntity forgetPsw = new ForgetPswRequestEntity();
        forgetPsw.setPhone(phone);
        forgetPsw.setCode(code);
        forgetPsw.setPassWord(passWord);
        return apiService.forgetPwd(forgetPsw);
    }

    @Override
    public Observable<BaseResponse<Boolean>> getModifyDepart(String id) {
        ModifyDepartRequestEntity modifydepart = new ModifyDepartRequestEntity();
        modifydepart.setId(id);
        return apiService.modifyDepart(modifydepart);
    }

    @Override
    public Observable<BaseResponse<Boolean>> setUserSex(String sex) {
        SexRequestEntity mSexRequestEntity = new SexRequestEntity();
        mSexRequestEntity.setSex(sex);
        return apiService.setUserSex(mSexRequestEntity);
    }

    @Override
    public Observable<BaseResponse<String>> sendSmsCode(String phone) {
        SendSmsCodeRequestEntity sendsmscode = new SendSmsCodeRequestEntity();
        sendsmscode.setPhone(phone);
        return apiService.sendSmsCode(sendsmscode);
    }

    @Override
    public Observable<BaseResponse<GetLeaveListResponseEntity>> getLeaveList(int currentPage, int pageSize) {
        GetLeaveListRequestEntity getleavelist = new GetLeaveListRequestEntity();
        getleavelist.setCurrentPage(currentPage);
        getleavelist.setPageSize(pageSize);
        return apiService.getLeaveList(getleavelist);
    }

    @Override
    public Observable<BaseResponse<List<SelectDepartmentResponseEntity>>> getDepartList() {
        return apiService.getDepartList();
    }

    @Override
    public Observable<BaseResponse<List<GetChatUsersResponseEntity>>> getChatUsers() {
        return apiService.getChatUsers();
    }

    /**
     * 删除联系人
     * @param frindId
     * @return
     */
    public Observable<BaseResponse<String>> DelectUsers(String frindId){
        DelectFriendEntity mo = new DelectFriendEntity();
        mo.setFriendId(frindId);
        return apiService.DelectUsers(mo);
    }

    @Override
    public Observable<BaseResponse<List<GetHistoryMsgResponseEntity>>> getHistoryMsg(String receiverId,
                                                                                     String endTime, String startTime) {
        GetHistoryMsgRequestEntity entity = new GetHistoryMsgRequestEntity();
        entity.setReceiverId(receiverId);
        entity.setEndTime(endTime);
        entity.setStartTime(startTime);
        return apiService.getHistoryMsg(entity);
    }

    @Override
    public Observable<BaseResponse<String>> sendMsg(String receiverId,
                                                    String content, String messageType) {
        SendMsgRequestEntity entity = new SendMsgRequestEntity();
        entity.setReceiverId(receiverId);
        entity.setContent(content);
        entity.setMessageType(messageType);
        return apiService.sendMsg(entity);
    }

    @Override
    public Observable<BaseResponse<String>> setHasRead(String receiverId) {
        SetHasReadRequestEntity entity = new SetHasReadRequestEntity();
        entity.setSenderId(receiverId);
        return apiService.setHasRead(entity);
    }

    @Override
    public Observable<BaseResponse<List<GetNewMsgResponseEntity>>> getNewMsg() {
        return apiService.getNewMsg();
    }

    @Override
    public Observable<BaseResponse<String>> addLeaveForm(String starTime, String endTime, String reason,
                                                          String typeId, String image, String address) {
        AddLeaveFormRequestEntity entity = new AddLeaveFormRequestEntity();
        entity.setStarTime(starTime);
        entity.setEndTime(endTime);
        entity.setReason(reason);
        entity.setTypeId(typeId);
        entity.setImage(image);
        entity.setAddress(address);
        return apiService.addLeaveForm(entity);
    }

    @Override
    public Observable<BaseResponse<ArrayList<GetBannerListResponseEntity>>> getGetBannerList() {
        return apiService.getBannerList();
    }

    @Override
    public Observable<BaseResponse<ArrayList<GetTypeListResponseEntity>>> getTypeList() {
        return apiService.getTypeList();
    }

    @Override
    public Observable<BaseResponse<String>> uploadImg(String path) {
        File file = new File(path);
        MultipartBody.Builder req = new MultipartBody.Builder().setType(MultipartBody.FORM);
        req.addFormDataPart("formFile", file.getName(), RequestBody.create(MediaType.parse(/*"application/octet-stream"*/"image/jpeg"), file));
        return apiService.upLoadImg(req.build().parts());
    }

    @Override
    public Observable<BaseResponse<Boolean>> uploadHead(String path) {
        UploadHeadRequestEntity entity = new UploadHeadRequestEntity();
        entity.setAvatar(path);
        return apiService.uploadHead(entity);
    }

    @Override
    public Observable<BaseResponse<String>> getCompanyInfo() {
        return apiService.getCompanyInfo();
    }

    @Override
    public Observable<BaseResponse<CompanyResponseEntity>> getCompany() {
        return apiService.GetCompany();
    }

    @Override
    public Observable<BaseResponse<VersionResponseEntity>> getCheckVersion() {
        return apiService.getCheckVersion();
    }

    public static RequestBody toRequestBody(String value) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), value);
        return requestBody;
    }


}
